//
//  ChapterViewController.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/14.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

class ChapterViewController: UIViewController {
//正倒序
    fileprivate var isPositive: Bool = false
    weak var delegate: ComicViewWillEndDraggingDelegate?
    var detailStatic: DetailStaticModel?
    var detailRealtime: DetailRealtimeModel?
    
    fileprivate lazy var collectionView: UICollectionView = {
       let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10)
        layout.minimumInteritemSpacing = 5
        layout.minimumLineSpacing = 10
        layout.itemSize = CGSize(width: (kSCREEN_WIDTH - 30)/2, height: 40)
        let collectionV = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionV.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        collectionV.alwaysBounceVertical = true
        collectionV.delegate = self
        collectionV.dataSource = self
        collectionV.register(ChapterCell.self, forCellWithReuseIdentifier: "ChapterCell")
        collectionV.register(ChapterHead.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "ChapterHead")
        return collectionV
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(self.collectionView)
        setUpSnap()
        // Do any additional setup after loading the view.
    }

    fileprivate func setUpSnap() {
        self.collectionView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view.usnp.edges)
        }
    }
    
    func chapterReloadData() {
        self.collectionView.reloadData()
    }

}

extension ChapterViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        delegate?.comicWillEndDragging(scrollView)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: kSCREEN_WIDTH, height: 44)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let head =  collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "ChapterHead", for: indexPath) as! ChapterHead
        head.model = detailStatic
        head.sortClouse {[weak self] (button) in
            if self?.isPositive == true {
                self?.isPositive = false
                button.setTitle("正序", for: .normal)
            } else {
                self?.isPositive = true
                button.setTitle("倒序", for: .normal)
            }
            self?.collectionView.reloadData()
        }
        return head
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return detailStatic?.chapter_list.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ChapterCell", for: indexPath) as! ChapterCell
        if isPositive {
            cell.model = detailStatic?.chapter_list[indexPath.row]
        }else {
            cell.model = detailStatic?.chapter_list.reversed()[indexPath.row]
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.collectionView.deselectItem(at: indexPath, animated: true)
        let index = isPositive ? indexPath.row : ((detailStatic?.chapter_list.count)! - indexPath.row - 1)
        let vc = ReadViewController(detailStatic: detailStatic, selectIndex: index)
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}




